<?php	
		function dangonline()
		{
				$sql="SELECT 	sum( if( username <>'' , 1 , 0 ) ) As SoThanhVien, 
				sum( if( username ='' , 1 , 0 ) ) As SoKhach , 
				count(*) As TongSoNguoi 
			FROM sessions";
			$kq=mysql_query($sql);
			$row=mysql_fetch_assoc($kq);
			return $row['TongSoNguoi'];
		}

		function getRealIpAddr()
		{
			if (!empty($_SERVER['HTTP_CLIENT_IP']))  //check ip from share internet
			{
			  $ip=$_SERVER['HTTP_CLIENT_IP'];
			}
			elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   
			//to check ip is pass from proxy
			{
			  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
			}
			else
			{
			  $ip=$_SERVER['REMOTE_ADDR'];
			}
			return $ip;
		}
		function getBrowserinfo(){
			$browserInfo=array(	'name'=>'',
								'version'=>'');
			$aBrowser=array('Firefox'=>'','MSIE'=>'','Chrome'=>'','Opera'=>'','Safari'=>'');
			$subject=$_SERVER['HTTP_USER_AGENT'];
			
			foreach($aBrowser as $key => $info)
			{
				$pattern = '#'.$key.'#imsU';
				if(preg_match($pattern, $subject, $matches))
				{
					$browserInfo['name']=$key;
					if($key=='MSIE')
					{
						$pattern_ = '#(?<=MSIE )[0-9.]+#';
						preg_match($pattern_, $subject, $matche);
						$browserInfo['version']=$matche[0];
					}
					else{
						$pattern_ = '#(?<='.$key.'/)[0-9.]+#';
						preg_match($pattern_, $subject, $matche);
						$browserInfo['version']=$matche[0];						
					}
					return $browserInfo; //array
				}
			}
			return null;
		}
	
		function getCountofWeek($day){
			$date['first'] = mktime(0,0,0,date("m"),date("d",$day)-date("w",$day),date("Y"));
			$date['last'] = mktime(0,0,0,date("m"),date("d",$day)+(6-date("w",$day)),date("Y"));
			return $date;
		}
		
		if(!isset($_SESSION['counter1']))
		{
				
			$sql="insert into counter1 values('','".getRealIpAddr()."',now(),'".$_SERVER['HTTP_USER_AGENT']."')";
			$result=mysql_query($sql);
			if($result){
			
				$sql="select count(*) as allcounter from counter1";
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
					$_SESSION['counter1']['allcounter']=$row['allcounter']+100000;
				
				$sql="select count(*) as today from counter1 where DATE(timelogin) = CURDATE()";
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
				$_SESSION['counter1']['today']=$row['today'];
				
				$yesterday =date("Y-m-d",strtotime(date('Y')."-".date('m')."-".date('d'))-86400);
				//echo $yesterday;die;
				$sql="select count(*) as yesterday from counter1 where DATE(timelogin)='".$yesterday."'";
				//echo $sql;die;
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
				$_SESSION['counter1']['yesterday']=$row['yesterday'];
				
				$dow=getCountofWeek(mktime(0,0,0,date("m"),date("d"),date("Y")));
				$sql="select count(*) as thisweek from counter1 where timelogin>='".date('Y-m-d',$dow['first'])."' and timelogin<='".date('Y-m-d',$dow['last'])."'";
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
				$_SESSION['counter1']['thisweek']=$row['thisweek'];				
				
				$dow=getCountofWeek(mktime(0,0,0,date("m"),date("d")-7,date("Y")));
				$sql="select count(*) as lastweek from counter1 where timelogin>='".date('Y-m-d',$dow['first'])."' and timelogin<='".date('Y-m-d',$dow['last'])."'";
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
				$_SESSION['counter1']['lastweek']=$row['lastweek'];				
				
				$sql="select count(*) as thismonth from counter1 where month(timelogin)=".date('m');
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
				$_SESSION['counter1']['thismonth']=$row['thismonth'];
				
				$lastmonth = mktime(0,0,0,date("m")-1,date("d"),date("Y"));
				$sql="select count(*) as lastmonth from counter1 where month(timelogin)=".date('m',$lastmonth);
				$result=mysql_query($sql);
				$row=mysql_fetch_array($result);
				$_SESSION['counter1']['lastmonth']=$row['lastmonth'];
				//$_SESSION['counter']['ipadd']=getRealIpAddr();
				//$bw=getBrowserinfo();
				//$_SESSION['counter']['browser']=$bw['name'].' version:' .$bw['version'];
				
////////////////////////////////////////// HIEN DU LIEU RA BAN COUTER //////////////////////				
///				/*
////				<tr>
///						<td><img src='images/v-today.jpg'> Đang online</td>
///						<td><div>".online()."</div></td>
///				  </tr>
//				<tr>
//					<td><img src='images/v-yesterday.jpg'> Hôm qua</td>
//					<td><div>".$_SESSION['counter1']['yesterday']."</div></td>
//				  </tr>
////				*/
///					 <tr>
//					<td><img src='images/v-week.jpg'> Tuần trước</td>
//					<td><div>".$_SESSION['counter1']['lastweek']."</div></td>
//				  </tr>
//
//
// <tr>
//					<td><img src='images/v-month.jpg'> Tháng trước</td>
//					<td><div>".$_SESSION['counter1']['lastmonth']."</div></td>
//				  </tr>
//
///////////////////////////////////////////////////////////////////////////////////////

	$t=$_SESSION['counter1']['allcounter'];
	
	$j=6;
	if($t>=1000000&&$t<10000000) $j=7;
	if($t>=10000000&&$t<100000000) $j=8;
	$i=$j;
	while($i>0)
	{
		$d=$t%10;
		$array[$i]=$d;
		$i--;
		$t=intval($t/10);
	}
	$hitcount='';
			$i=1;
			while($i<=$j)
			{
			if($array[$i]==0)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/0.png'>";
			}
		 	if($array[$i]==1)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/1.png'>";
			}

			if($array[$i]==2)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/2.png'>";
			}

if($array[$i]==3)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/3.png'>";
			}

if($array[$i]==4)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/4.png'>";
			}

if($array[$i]==5)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/5.png'>";
			}


if($array[$i]==6)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/6.png'>";
			}

if($array[$i]==7)
			{
				$hitcount.="<img src='".$duongdan."images/counter/7.png'>";
			}

if($array[$i]==8)
			{
				$hitcount.="<img src='".$duongdan."images/counter/8.png'>";
			}

if($array[$i]==9)
			{
				$hitcount.="<img src='".$duongdan."images/counter/9.png'>";
			}
$i++;}
				echo"
				<table border='0' cellspacing='0' cellpadding='1' width='100%' style='padding-left:4px;' >
					 <tr>
					<td>Hôm nay</td>
					<td><div align='left' style='color:#006480;font-weight:bold;' >".$_SESSION['counter1']['today']."</div></td>
				  </tr>
				   <tr>
					<td>Hôm qua</td>
					<td><div align='left' style='color:#006480;font-weight:bold;' >".$_SESSION['counter1']['yesterday']."</div></td>
				  </tr>
				  <tr>
					<td>Tổng lượt truy cập</td>
					<td><div align='left' style='color:#006480;font-weight:bold;' >".$_SESSION['counter1']['allcounter']."</div></td>
				  </tr>
				</table>
				";
			}
			else
			{
				echo "loi insert";
			}
		}
		else{
		$t=$_SESSION['counter1']['allcounter'];
	
	
	$j=6;
	if($t>=1000000&&$t<10000000) $j=7;
	if($t>=10000000&&$t<100000000) $j=8;
	$i=$j;
	while($i>0)
	{
		$d=$t%10;
		$array[$i]=$d;
		$i--;
		$t=intval($t/10);
	}
	$hitcount='';
			$i=1;
			while($i<=$j)
			{
			if($array[$i]==0)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/0.png'>";
			}
		 	if($array[$i]==1)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/1.png'>";
			}

			if($array[$i]==2)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/2.png'>";
			}

if($array[$i]==3)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/3.png'>";
			}

if($array[$i]==4)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/4.png'>";
			}

if($array[$i]==5)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/5.png'>";
			}


if($array[$i]==6)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/6.png'>";
			}

if($array[$i]==7)
			{
				$hitcount.="<img src='".$duongdan."images/counter/7.png'>";
			}

if($array[$i]==8)
			{
				$hitcount.="<img src='".$duongdan."images/counter/8.png'>";
			}

if($array[$i]==9)
			{
				$hitcount.="<img src='".$duongdan."images/counter/9.png'>";
			}
$i++;}
			echo"
				<table border='0' cellspacing='0' cellpadding='1' width='100%' style='padding-left:4px;' >
					 <tr>
					<td>Hôm nay</td>
					<td><div align='left' style='color:#006480;font-weight:bold;' >".$_SESSION['counter1']['today']."</div></td>
				  </tr>
				   <tr>
					<td>Hôm qua</td>
					<td><div align='left' style='color:#006480;font-weight:bold;' >".$_SESSION['counter1']['yesterday']."</div></td>
				  </tr>
				  <tr>
					<td>Tổng lượt truy cập</td>
					<td><div align='left' style='color:#006480;font-weight:bold;' >".$_SESSION['counter1']['allcounter']."</div></td>
				  </tr>
				</table>
				";
		}

?>